# -*- encoding: utf-8 -*-
from django.conf import settings

from filebrowser import settings as fb_settings
import re
import os

def get_images(path):
    # Precompile regular expressions
    filter_re = []
    for exp in fb_settings.EXCLUDE:
        filter_re.append(re.compile(exp))
    for k,v in fb_settings.VERSIONS.iteritems():
        exp = (r'_%s.(%s)') % (k, '|'.join(fb_settings.EXTENSION_LIST))
        filter_re.append(re.compile(exp))

    # List images
    PATH = os.path.join('uploads', path)
    DIRECTORY = os.path.join(settings.MEDIA_ROOT, PATH)
    dir_list = [os.path.normcase(f) for f in os.listdir(DIRECTORY)]
    dir_list.sort()
    files = []

    for file in dir_list:
        # EXCLUDE FILES MATCHING VERSIONS_PREFIX OR ANY OF THE EXCLUDE PATTERNS
        filtered = file.startswith('.')
        for re_prefix in filter_re:
            if re_prefix.search(file):
                filtered = True
        if filtered:
            continue
        
        if os.path.splitext(file)[1] in ('.jpg', '.jpeg'):
            image = os.path.join(PATH, file)
            files.append(image)
            
    return files
